package com.mes.inventory.dao;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.bstek.dorado.data.provider.Page;
import com.mes.common.dao.BasicDao;
import com.mes.inventory.dao.iface.IInvIODao;
import com.mes.inventory.domain.InvIO;

@Repository
public class InvIODao extends BasicDao<InvIO> implements IInvIODao {

	@Override
	public void getInvIOs(Page<InvIO> page, Map<String, Object> params) throws Exception {
		// TODO Auto-generated method stub
		if (null != params&&params.size()>0) {
			Map<String, Object> args = new HashMap<String, Object>();
			StringBuffer coreHql = new StringBuffer("from " + InvIO.class.getName() + " a where 1=1 ");

			Date beginDate = (Date) params.get("beginDate");
			Date endDate = (Date) params.get("endDate");
			if (null != beginDate && null != endDate) {
				coreHql.append(" and a.createTime between :beginDate and :endDate ");
				args.put("beginDate", beginDate);
				args.put("endDate", endDate);
			}

			String invIOId = (String) params.get("invIOId");
			if (StringUtils.isNotEmpty(invIOId)) {
				coreHql.append(" and a.invIOId =:invIOId ");
				args.put("invIOId", invIOId);
			}

			String ioEmployeeUid = (String) params.get("ioEmployeeUid");
			if (StringUtils.isNotEmpty(ioEmployeeUid)) {
				coreHql.append(" and a.ioEmployeeUid =:ioEmployeeUid ");
				args.put("ioEmployeeUid", ioEmployeeUid);
			}

			String creatorUid = (String) params.get("creatorUid");
			if (StringUtils.isNotEmpty(creatorUid)) {
				coreHql.append(" and a.creatorUid =:creatorUid ");
				args.put("creatorUid", creatorUid);
			}

			Integer ioState = (Integer) params.get("ioState");
			if (null != ioState) {
				coreHql.append(" and a.ioState =:ioState ");
				args.put("ioState", ioState);
			}

			String strIoType = (String) params.get("ioType");
			if (StringUtils.isNotEmpty(strIoType)) {
				Integer ioType = Integer.valueOf(strIoType);
				coreHql.append(" and a.ioType =:ioType ");
				args.put("ioType", ioType);
			}

			String warehouseUid = (String) params.get("warehouseUid");
			if (StringUtils.isNotEmpty(warehouseUid)) {
				coreHql.append(" and a.warehouseUid =:warehouseUid ");
				args.put("warehouseUid", warehouseUid);
			}

			String countHql = "select count(*) " + coreHql.toString();
			String hql = coreHql.toString();

			Map<String, Object> newArgs = new HashMap<String, Object>();
			String key;
			String newKey;
			Iterator<String> iterator = args.keySet().iterator();
			while (iterator.hasNext()) {
				key = (String) iterator.next();
				newKey = key.replace(".", "_");
				newArgs.put(newKey, args.get(key));
				hql = hql.replace(":" + key, ":" + newKey);
				countHql = countHql.replace(":" + key, ":" + newKey);

				System.out.println("hql: " + hql);
				System.out.println("countHql: " + countHql);
				System.out.println("newArgs: " + newArgs);
			}
			this.pagingQuery(page, hql, countHql, newArgs);
		}
	}

	@Override
	public List<InvIO> getInvIosByTaskUidIOType(String taskUid, int i) {
		// TODO Auto-generated method stub

		return null;
	}
}
